package com.helper.email;

import com.helper.email.model.EmailDto;
import com.helper.email.model.HtmlEmailDto;
import org.apache.commons.mail.*;

public class EmailUtil {

    public static void email(EmailDto mailCommon) {
        Email commonEmail = new SimpleEmail();
        commonEmail.setHostName(mailCommon.getEmailHost().hostName);
        commonEmail.setSmtpPort(mailCommon.getEmailHost().smsPort);
        commonEmail.setAuthenticator(new DefaultAuthenticator(mailCommon.getEmailAddr(), mailCommon.getAuthorizationCode()));
        commonEmail.setSSLOnConnect(true);
        commonEmail.setCharset("UTF-8");
        try {
            commonEmail.setFrom(mailCommon.getEmailAddr());
            commonEmail.setSubject(mailCommon.getSubject());
            commonEmail.setMsg(mailCommon.getContext());
            commonEmail.addTo(mailCommon.getToEmailAddr());
            commonEmail.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

    public static void htmlEmail(HtmlEmailDto htmlEmailDto) {
        HtmlEmail commonEmail = new HtmlEmail();
        commonEmail.setHostName(htmlEmailDto.getEmailHost().hostName);
        commonEmail.setSmtpPort(htmlEmailDto.getEmailHost().smsPort);
        commonEmail.setAuthenticator(new DefaultAuthenticator(htmlEmailDto.getEmailAddr(), htmlEmailDto.getAuthorizationCode()));
        commonEmail.setSSLOnConnect(true);
        commonEmail.setCharset("UTF-8");
        try {
            commonEmail.setFrom(htmlEmailDto.getEmailAddr());
            commonEmail.setSubject(htmlEmailDto.getSubject());
            commonEmail.setTextMsg(htmlEmailDto.getMsgText());
            commonEmail.setHtmlMsg(htmlEmailDto.getMsgHtml());
            commonEmail.addTo(htmlEmailDto.getToEmailAddr());
            commonEmail.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }

}
